import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

// 盘源
const houseTypeRange = [{
  id: 'workshop',
  name: '厂房'
}, {
  id: 'warehouse',
  name: '仓库'
}, {
  id: 'land',
  name: '空地'
}]

// 消防等级
const fireRateRange = [{
  id: '甲',
  name: '甲类'
}, {
  id: '乙',
  name: '乙类'
}, {
  id: '丙',
  name: '丙类'
}, {
  id: '丙二',
  name: '丙二类'
}, {
  id: '丁',
  name: '丁类'
}]

// 购买形式
const sellRange = [{
  id: 'workshop',
  name: '求租'
}, {
  id: 'warehouse',
  name: '求购'
}]

export default {
  namespace: 'searchMatch',
  state: {
    houseTypeRange, // 盘源类型
    houseType: { // 盘源值
      id: '',
      name: ''
    },
    fireRateRange, // 消防等级
    fireRate: { // 消防等级值
      id: '',
      name: ''
    },
    sellRange, // 购买形式
    sellType: { // 购买形式值
      id: '',
      name: ''
    },
    districtTypeRange: [], // 区域下拉
    districtType: { // 区域值
      id: 0,
      name: '区域'
    },
    minArea: '', // 最小面积
    maxArea: '', // 最大面积
    floor: '', // 楼层
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    },
    initData(state, action) {
      return {
        ...state,
        ...action.payload,
        nowType: {
          value: 0,
          label: '求职'
        },
        districtType: {
          id: 0,
          name: '不限'
        },
        contact: "",
        cellPhone: "",
        title: "",
        description: "",
        endData: "",
      }
    }
  },
  effects: {
    // 获得区域信息
    * loadDistrict({payload = {}}, {put, call}) {
      const {data} = yield call(request, {
        url: SERVERURL + '/service/district/all',
        method: 'get',
      })
      if (data && data.data instanceof Array) {
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            districtTypeRange: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
  }
};
